package com.ramos.kuaixiao.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.utils.CommonLoading;
import com.ramos.kuaixiao.utils.NetUtils;
import com.ramos.kuaixiao.utils.SharedPreference;
import com.ramos.kuaixiao.utils.StringUtil;
import com.ramos.kuaixiao.utils.ToastUtils;

import org.apache.http.Header;
import org.apache.http.entity.ByteArrayEntity;
import org.json.JSONException;
import org.json.JSONObject;


public class EditUserInfoActivity extends BaseActivity implements OnClickListener {
    private final static String TAG = "EditUserInfoActivity";
    private Context mContext;
    private ImageView mIconImage;
    private EditText shop_id;
    private EditText shop_name;
    private EditText shop_invert;
    private EditText shop_pw;
    private EditText shop_pw_cf;
    private EditText shop_people;
    private EditText shop_phone;
    private EditText shop_address;
    private EditText shop_email;
    private EditText shop_introduce;
    private Button mRegisterButton;
    private String mIdStr = "";
    private String mInvertStr = "";
    private String mNameStr = "";
    private String mPwStr = "";
    private String mPwCfStr = "";
    private String mPeopleStr = "";
    private String mPhoneStr = "";
    private String mAddressStr = "";
    private String mEmailStr = "";
    private String mIntroduceStr = "";
    private LinearLayout title_reback;
    private TextView tv_aboutus;
    CommonLoading loading;
    public final static int UPLOAD_SUCCESS = 100;
    public final static int UPLOAD_FAIL = 101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_userinfo);
        initView();
    }

    private void initView() {
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.title), false);
        mContext = this;
        tv_aboutus = (TextView) findViewById(R.id.title_text);
        tv_aboutus.setText("用户注册");
        title_reback = (LinearLayout) findViewById(R.id.title_reback);
        title_reback.setOnClickListener(this);

        mIconImage = (ImageView) findViewById(R.id.activity_detail_info_regist_headimg);
        shop_id = (EditText) findViewById(R.id.shop_id);
        shop_name = (EditText) findViewById(R.id.shop_name);
        shop_invert = (EditText) findViewById(R.id.shop_invert);
        shop_pw = (EditText) findViewById(R.id.shop_pw);
        shop_pw_cf = (EditText) findViewById(R.id.shop_pw_cf);
        shop_people = (EditText) findViewById(R.id.shop_people);
        shop_phone = (EditText) findViewById(R.id.shop_phone);
        shop_address = (EditText) findViewById(R.id.shop_address);
        shop_email = (EditText) findViewById(R.id.shop_email);
        shop_introduce = (EditText) findViewById(R.id.shop_introduce);

        mRegisterButton = (Button) findViewById(R.id.activity_detail_info_regist_regist);
        mIconImage.setOnClickListener(this);
        mRegisterButton.setOnClickListener(this);
    }

    private void registerUser() {
        mIdStr = shop_id.getText().toString();
        mNameStr = shop_name.getText().toString();
        mInvertStr = shop_invert.getText().toString();
        mPwStr = shop_pw.getText().toString();
        mPwCfStr = shop_pw_cf.getText().toString();
        mPeopleStr = shop_people.getText().toString();
        mPhoneStr = shop_phone.getText().toString();
        mAddressStr = shop_address.getText().toString();
        mEmailStr = shop_email.getText().toString();
        mIntroduceStr = shop_introduce.getText().toString();
        //Utils.validateUser(mIdStr, mNameStr, mPwStr, mPwCfStr, mPeopleStr, mPhoneStr, mAddressStr, mEmailStr, mIntroduceStr);
        if (StringUtil.isEmpty(mIdStr)) {
            ToastUtils.show("请输入账号");
            return;
        }
        if (StringUtil.isEmpty(mNameStr)) {
            ToastUtils.show("请输入名称");
            return;
        }
        if (StringUtil.isEmpty(mInvertStr)) {
            ToastUtils.show("请输入邀请码");
            return;
        }
        if (StringUtil.isEmpty(mPwStr)) {
            ToastUtils.show("请输入密码");
            return;
        }
        if (StringUtil.isEmpty(mPwCfStr)) {
            ToastUtils.show("请确认密码");
            return;
        }
        if (StringUtil.isEmpty(mPeopleStr)) {
            ToastUtils.show("请填入联系人");
            return;
        }
        if (StringUtil.isEmpty(mPhoneStr)) {
            ToastUtils.show("请填入电话");
            return;
        }
        if (StringUtil.isEmpty(mAddressStr)) {
            ToastUtils.show("请填入地址");
            return;
        }
        if (!(mPwStr.equals(mPwCfStr))) {
            ToastUtils.show("输入密码不一致");
            return;
        }
        try {
            String url = NetUtils.REGISTER_DEALER;
            loading = new CommonLoading(this);
            loading.setLoadText("注册中...");
            AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
            // 创建请求参数的封装的对象
            client.setTimeout(10000);
            // 创建请求参数的封装的对象
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userId", SharedPreference.getPreferString("id"));
            jsonObject.put("id", mIdStr);
            jsonObject.put("name", mNameStr);
            jsonObject.put("password", mPwStr);
            jsonObject.put("leader", mPeopleStr);
            jsonObject.put("telephone", mPhoneStr);
            jsonObject.put("addr", mAddressStr);
            jsonObject.put("email", mEmailStr);
            jsonObject.put("introduction", mIntroduceStr);
            jsonObject.put("disabled", 1);
            Log.d(TAG, "login " + url);
            Log.d(TAG, "login " + jsonObject.toString());
            ByteArrayEntity stringEntity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
            stringEntity.setContentType("application/json");
            // 执行post方法
            client.post(mContext, url, stringEntity, "application/json", new JsonHttpResponseHandler() {

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject responseBody) {
                    loading.dismiss();
                    Log.d(TAG, "login " + responseBody.toString());
                    try {
                        if (responseBody.getInt("status") == 200) {
                            sendMessagea(UPLOAD_SUCCESS, responseBody.getString("msg"));
                        } else {
                            sendMessagea(UPLOAD_FAIL, responseBody.getString("msg"));
                        }
                    } catch (JSONException e) {
                        sendMessagea(UPLOAD_FAIL, "注册异常");
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                    loading.dismiss();
                    sendMessagea(UPLOAD_FAIL, "注册异常");
                    throwable.printStackTrace();// 把错误信息打印出轨迹来
                }
            });
        } catch (Exception e) {
            loading.dismiss();
            sendMessagea(UPLOAD_FAIL, "注册异常");
            e.printStackTrace();
        }
    }

    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPLOAD_SUCCESS:
                    ToastUtils.show("注册成功");
                    startActivity(new Intent(mContext, LoginActivity.class));
                    finish();
                    break;
                case UPLOAD_FAIL:
                    ToastUtils.show(msg.obj.toString());
                    break;
                default:
                    break;
            }
        }
    };

    private void sendMessagea(int iWhat, Object obj) {
        Message msg = Message.obtain();
        msg.what = iWhat;
        msg.obj = obj;
        mHandler.sendMessage(msg);
    }


    @Override
    public void onClick(View paramView) {
        switch (paramView.getId()) {
            case R.id.title_reback:
                finish();
                break;
            case R.id.activity_detail_info_regist_regist:
                registerUser();
                break;
            default:
                break;
        }
    }
}
